lunorecipes = { }

if minetest.setting_getbool("lunomobs_quests")~=true then 
	minetest.setting_set("lunomobs_quests","false")
end



lunorecipes.doEat = function(player, itemstack, addHP, addStamin, sound, addItem)
--lunorecipes.doEat = function(player, addHP, objID, sound)
	if player~=nil and player:is_player() then
		local playername = player:get_player_name()
		if itemstack~=nil and itemstack:get_name()~=nil and itemstack:get_name()~="" then
			local objID = itemstack:get_name()
			local objDesc = minetest.registered_items[objID].description

			if 
				(
					addHP~=nil and type(addHP)=="number" and addHP~=0 and player:get_hp()<20
				) or (
					addHP~=nil and type(addHP)=="number" and addHP<0  -- para venenos!
				) or (
					addStamin~=nil and type(addStamin)=="number" and addStamin~=0 and 
					lunogauges~=nil and lunogauges.huds.get_stamin~=nil and lunogauges.huds.get_stamin(playername)<20
				)
			then
				if not minetest.setting_getbool("creative_mode") then 
					itemstack:take_item()
					if addItem~=nil and type(addItem)=="string" and addItem~="" and minetest.registered_items[addItem]~=nil then
						player:get_inventory():add_item("main", ItemStack(addItem))
					end
				end
				
				if addHP~=nil and type(addHP)=="number" and addHP~=0 then
					local newHP = player:get_hp() + addHP
					if newHP > 20 then newHP = 20 end
					player:set_hp(newHP)
				end

				if addStamin~=nil and type(addStamin)=="number" and addStamin~=0 and lunogauges~=nil and lunogauges.huds.get_stamin~=nil then
					local stamin = lunogauges.huds.get_stamin(playername)
					lunogauges.huds.set_stamin(playername, stamin + addStamin)
				end
			
				if minetest.setting_getbool("lunomobs_quests") then
					if objID~=nil and objID~="" then
						--objID = objID:gsub(":","_-_")
						local qtdObjectsAntiga = lunomods.getCharValue(playername, "eat("..objID..")") or 0
						local qtdObjectsNova = qtdObjectsAntiga + 1
						lunomods.setCharValue(playername, "eat("..objID..")", qtdObjectsNova)
					else
						print("############################### [ERRO] Valor 'lunorecipes.doContEat.objID' invalido!")
					end
				end

				if sound~=nil and type(sound)=="string" and sound~="" and player~=nil and player:is_player() then
					minetest.sound_play(sound, {
						pos=player:getpos(),
						gain = 6.0,
						max_hear_distance = 5,
					})
				end
			else
				minetest.chat_send_player(playername, "Voce nao pode consumir '"..objDesc.."' quando estiver cheio!")
			end
			return itemstack
		else
			print("############################### [ERRO] Valor 'lunorecipes.doContEat.itemstack' invalido!")
		end
	else
		print("############################### [ERRO] Valor 'lunorecipes.doContEat.player' invalido!")
	end
end
